package demo.icanci.pattern.singleton;

/**
 * 单例设计模式 - 懒汉式：双重检查锁
 *
 * @author icanci
 * @date Created in 2021/02/06 00:18
 * @version 1.0
 */
public class LazyDoubleCheckSingleton {
    /**
     * 初始化
     */
    private static volatile LazyDoubleCheckSingleton INSTANCE;

    /**
     * 私有化构造
     */
    private LazyDoubleCheckSingleton() {

    }

    /**
     * 获取单例对象
     * @return 返回单例对象
     */
    public static LazyDoubleCheckSingleton getInstance() {
        if (null == INSTANCE) {
            synchronized (LazyDoubleCheckSingleton.class) {
                if (null == INSTANCE) {
                    // memory = allocate() 分配内存对象
                    // instance(memory) 初始化对象
                    // instance = memory 设置instance指向刚刚分配的地址 此时 instance != null
                    INSTANCE = new LazyDoubleCheckSingleton();
                }
            }
        }
        return INSTANCE;
    }

    public static void main(String[] args) {
        System.out.println(LazyDoubleCheckSingleton.getInstance() == LazyDoubleCheckSingleton.getInstance());
    }
}
